package com.qf.controller;

import com.qf.dto.DeveloperDto;
import com.qf.model.Project;
import com.qf.model.Stage;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.page.Result;
import com.qf.service.ProjectService;
import com.qf.service.StageService;
import com.qf.vo.DeveloperVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/stage")
public class StageController {

    @Autowired
    private StageService stageService;

    /**
     * stage列表分页查询+模糊查询
     * @param queryPageBean
     * @return PageResult
     */
    @RequestMapping("/all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = stageService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 新建开发商并绑定工程
     * @param
     * @return Result
     */
    //前端传json
    @RequestMapping("/add")
    public Result add(@RequestBody Stage stage) {
        Result result = new Result();
        boolean b = stageService.add(stage);
        if (b){
            result.setFlag(true);
            result.setMessage("添加成功");
            return result;
        }
        result.setFlag(false);
        result.setMessage("添加失败");
        return result;
    }

    /**
     * 编辑stage回显数据
     * @param sid
     * @return Result
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam("sid") String sid ){
        Result result = new Result();
        Stage stage = stageService.findById(sid);

        if (ObjectUtils.isNotEmpty(stage)){
            result.setData(stage);
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }else {
            result.setFlag(false);
            result.setMessage("fail");
            return result;
        }
    }

    /**
     * 修改stage
     * @param stage
     * @return result
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Stage stage){
        Result result = new Result();
        int i = stageService.edit(stage);
        if (i>0){
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }

    /**
     * 删除stage
     * @param sid
     * @return result
     */
    @RequestMapping("/delete")
    public Result delete(@RequestParam("sid") String sid){
        Result result = new Result();
        int i = stageService.delete(sid);
        if (i>0){
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }
}
